package encome.work.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import encome.work.dao.Orders;
import encome.work.dao.ResponseResult;
import encome.work.utils.StatusCode;
import encome.work.service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author fyftless
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 新增订单
     */
    @PostMapping("/insert")
    public ResponseResult<String> add(@RequestBody Orders order){
        orderService.add(order);
        return new ResponseResult<>(StatusCode.OK, "新增订单成功");
    }

    /**
     * 更新订单
     */
    @PutMapping("/update/{id}")
    public ResponseResult<String> update(@PathVariable("id") Long id, @RequestBody Orders order){
        order.setId(id);
        orderService.update(order);
        return new ResponseResult<>(StatusCode.OK, "修改订单成功");
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult<String> delete(@PathVariable("id") Long id){
        orderService.delete(id);
        return new ResponseResult<>(StatusCode.OK, "删除订单成功");
    }

    /**
     * 多条件分页查询订单
     */
    @PostMapping("/search/{page}/{size}")
    public ResponseResult<Page<Orders>> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size,
                                                @RequestBody Orders order){
        Page<Orders> orderPage= orderService.findPage(order, page, size);
        return new ResponseResult<>(StatusCode.OK, "查询成功", orderPage);
    }
    /**
     * 根据id查询订单
     */
    @GetMapping("/{id}")
    public ResponseResult<Orders> findById(@PathVariable("id") Long id){
        Orders order = orderService.findById(id);
        return new ResponseResult<>(StatusCode.OK, "查询成功", order);
    }

    /**
     * 默认分页查询订单
     */
    @GetMapping("/search/{page}/{size}")
    public ResponseResult<Page<Orders>> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Page<Orders> orderPage = orderService.findPage(page, size);
        return new ResponseResult<>(StatusCode.OK,"查询成功", orderPage);
    }

    @GetMapping("/get/{id}")
    public ResponseResult<String> getOrder(@PathVariable Long id){
        orderService.getOrder(id);
        return new ResponseResult<>(StatusCode.OK, "接单成功");
    }

    @GetMapping("/confirm/{id}")
    public ResponseResult<String> getMedicine(@PathVariable Long id){
        orderService.getMedicine(id);
        return new ResponseResult<>(StatusCode.OK, "订单状态更改为取药成功");
    }

    @GetMapping("/finished/{id}")
    public ResponseResult<String> finishOrder(@PathVariable Long id){
        orderService.finishOrder(id);
        return new ResponseResult<>(StatusCode.OK, "订单成功完成");
    }

    @GetMapping("/cancel/{id}")
    public ResponseResult<String> cancel(@PathVariable Long id){
        orderService.cancelOrder(id);
        return new ResponseResult<>(StatusCode.OK, "取消订单成功");
    }

    @GetMapping("/cancelreceived/{id}")
    public ResponseResult<String> cancelByVolunteer(@PathVariable Long id){
        orderService.cancelOrderReceived(id);
        return new ResponseResult<>(StatusCode.OK, "取消接单成功");
    }
}

